package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.SnowFlakeUtil;
import com.ruoyi.mall.domain.vo.MallOrderDetailVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.vo.MallOrderVo;
import com.ruoyi.mall.domain.bo.MallOrderBo;
import com.ruoyi.mall.service.IMallOrderService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分商城订单主
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/order")
public class MallOrderController extends BaseController {

    private final IMallOrderService iMallOrderService;

    /**
     * 查询积分商城订单主列表
     */
    @SaCheckPermission("mall:order:list")
    @GetMapping("/list")
    public TableDataInfo<MallOrderVo> list(MallOrderBo bo, PageQuery pageQuery) {
        return iMallOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出积分商城订单主列表
     */
    @SaCheckPermission("mall:order:export")
    @Log(title = "积分商城订单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallOrderBo bo, HttpServletResponse response) {
        List<MallOrderVo> list = iMallOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "积分商城订单主", MallOrderVo.class, response);
    }

    /**
     * 获取积分商城订单主详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:order:query")
    @GetMapping("/{id}")
    public R<MallOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iMallOrderService.queryById(id));
    }

    /**
     * 新增积分商城订单主
     */
    @SaCheckPermission("mall:order:add")
    @Log(title = "积分商城订单主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallOrderBo bo) {
        return toAjax(iMallOrderService.insertByBo(bo));
    }

    /**
     * 修改积分商城订单主
     */
    @SaCheckPermission("mall:order:edit")
    @Log(title = "积分商城订单主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallOrderBo bo) {
        return toAjax(iMallOrderService.updateByBo(bo));
    }

    /**
     * 删除积分商城订单主
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:order:remove")
    @Log(title = "积分商城订单主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMallOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }



    /***
     * 根据用户信息来查询所有的订单信息
     * @param pageQuery
     * @return
     */
    @SaIgnore
    @RequestMapping("/getOrderListByCustomerId")
    public TableDataInfo<MallOrderVo> getOrderListByCustomerId(PageQuery pageQuery)
    {
       LoginUser loginUser = LoginHelper.getLoginUser();
       MallOrderBo query = new MallOrderBo();
       query.setCustomerId(loginUser.getUserId());
       return iMallOrderService.queryPageList(query, pageQuery);
    }

    /**
     * 根据订单ID查找所有的订单明细列表
     * @param map
     * @return
     */
    @SaIgnore
    @RequestMapping("/getOrderDetailByOrderId")
    public R<List<MallOrderDetailVo>> getOrderDetailByOrderId(@RequestBody Map<String, Object> map)
    {
        String orderId = map.get("orderId").toString();
        return R.ok(iMallOrderService.queryOrderDetailByOrderId(orderId));
    }
}
